using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using BoOnce.GameLogic.Entities;
using BoOnce.GameLogic;
using BoOnce.Helpers;
using BoOnce.Core.Components;

namespace BoOnce.Components
{
    class PlayerActionComponent : GameComponent
    {
        private InputComponent _Input;
        private Player[] _Players;
        private Game _Game;

        public PlayerActionComponent(Game game)
            : base(game)
        {
            _Game = game;
            _Players = new Player[Settings.MAX_PLAYER];

            for (int i = 0; i < Settings.MAX_PLAYER; i ++ )
            {
                _Players[i] = null;
            }
        }


        public void AddPlayer(Player player)
        {
            if ((int)player.ControllerIndex < Settings.MAX_PLAYER)
            {
                _Players[(int)player.ControllerIndex] = player;
            }
            else
            {
                Logger.WriteWarning(this,"AddPlayer outOfBound " + player.ControllerIndex + "/" + Settings.MAX_PLAYER);
            }
        }

        public void RemovePlayer(Player player)
        {
            if ((int)player.ControllerIndex < Settings.MAX_PLAYER)
            {
                _Players[(int)player.ControllerIndex] = null;
            }
            else
            {
                Logger.WriteWarning(this, "AddPlayer outOfBound " + player.ControllerIndex + "/" + Settings.MAX_PLAYER);
            }
        }

        public void ClearPlayers()
        {
            for(int i=0; i < Settings.MAX_PLAYER; i++)
            {    
                _Players[i] = null;
            }
        }

        public override void Initialize()
        {
            _Input = (InputComponent)_Game.Services.GetService(typeof(InputComponent));
            Logger.WriteInfo("PlayerActionComponent[#" + this.UpdateOrder + "] init");
        }

        public override void Update(GameTime gameTime)
        {
            for (int i = 0; i < Settings.MAX_PLAYER; i++)
            {
                if (_Players[i] != null)
                {
                    PlayerIndex playerIndex = _Players[i].ControllerIndex;
                    if (_Input.IsPress(playerIndex, InputComponent.Buttons.Left))
                    {
                        _Players[i].ButtonHandler(InputComponent.Buttons.Left, gameTime, _Input.IsNewPress(playerIndex, InputComponent.Buttons.Left));
                    }

                    if (_Input.IsPress(playerIndex, InputComponent.Buttons.Right))
                    {
                        _Players[i].ButtonHandler(InputComponent.Buttons.Right, gameTime, _Input.IsNewPress(playerIndex, InputComponent.Buttons.Right));
                    }

                    if (_Input.IsPress(playerIndex, InputComponent.Buttons.Up))
                    {
                        _Players[i].ButtonHandler(InputComponent.Buttons.Up, gameTime, _Input.IsNewPress(playerIndex, InputComponent.Buttons.Up));
                    }

                    if (_Input.IsPress(playerIndex, InputComponent.Buttons.Down))
                    {
                        _Players[i].ButtonHandler(InputComponent.Buttons.Down, gameTime, _Input.IsNewPress(playerIndex, InputComponent.Buttons.Down));
                    }

                    if (_Input.IsPress(playerIndex, InputComponent.Buttons.Action1))
                    {
                        _Players[i].ButtonHandler(InputComponent.Buttons.Action1, gameTime, _Input.IsNewPress(playerIndex, InputComponent.Buttons.Action1));
                    }
                }
            }
        }

    }
}
